package com.makinolo.rlvforge
{
	import com.google.maps.*;
	import com.google.maps.controls.*;
	import com.google.maps.overlays.*;
	import com.google.maps.styles.ButtonStyle;
	
	import flash.events.Event;
	import flash.geom.Point;

	public class RLVMap extends Map
	{
		public var app:RLVForge;
		private var trackline:Polyline; 
		private var mapposition:Marker;
		private var mapready:Boolean;		
		private var menuopts:Array = ["Relocate","Fix","Delete","Infobox", "Go to"];
		
		public function RLVMap()
		{
			super();
			addEventListener(MapEvent.MAP_READY, onMapReady);			
		}
		
		private function onMapReady(event:Event):void 
		{
			// Posicion inicial y controles del mapa
			this.addControl(new ZoomControl(new ZoomControlOptions( 
												{ buttonSize: new Point(16, 16),
		     									  buttonStyle: new ButtonStyle({ allStates: {bevelThickness: 1.5,
		         													  						 bevelAlpha: 0.5}
												 								}),
		     									  buttonSpacing: new Point(4, 4),
		     									  hasScrollTrack: false,
		     									  position: new ControlPosition(ControlPosition.ANCHOR_TOP_LEFT, 10, 10)
		   									})));
			this.addControl(new MapTypeControl());			
			mapready = true;
		}
		
		public function isMapReady():Boolean
		{
			return mapready;
		}

		public function trackClicked(e:MapMouseEvent):void
		{
			var point:LatLng = e.latLng;
			var index:int = app.getPdata().findNearestTrackPoint(point);
			var mm:MapMenu = new MapMenu(point, menuopts, 0);			
			var mo:MarkerOptions = new MarkerOptions( {icon: mm} ); 		
			var m:Marker = new Marker(app.getPdata().getPointAt(index),  mo);			
			this.addOverlay(m);
		}
		
		public function changePointTime(point:LatLng, recalc:Boolean):void
		{
			app.pausePlaying();	
			var index:int = app.getPdata().findNearestTrackPoint(point);		
			if (app.getPdata().updateGISPointTime(app.getVideoPlayer().playheadTime, index, recalc))
			{
				var m:Marker = new Marker(app.getPdata().getPointAt(index),  new MarkerOptions( { fillStyle: { color: 0x0055ff }, tooltip:"Time marker" , clickable: true  } ))
				m.addEventListener(MapMouseEvent.CLICK, deleteMarker);
				this.addOverlay(m);
			}	
		}	
		
		public function showPoint(e:MapMouseEvent):void
		{
			var point:LatLng = e.latLng;
			var index:int = app.getPdata().findNearestTrackPoint(point);
			var mm:MapMenu = new MapMenu(point,menuopts, 0);
			mm.addEventListener("MapMenuItemClicked", mapMenuListener);
			var mo:MarkerOptions = new MarkerOptions( {icon: mm,											   
													   fillStyle: {color: 0x5555ff },
													   tooltip:"Click for Menu, \n" + "Use first letter of option to select" , clickable: true  } ); 		
			var overPoint:Marker = new Marker(app.getPdata().getPointAt(index),  mo)
			overPoint.addEventListener(MapMouseEvent.ROLL_OUT, hidePoint);
			//mm.addEventListener(MapMouseEvent.ROLL_OUT, hidePoint);
			this.addOverlay(overPoint);				
		}
		
		public function hidePoint(e:MapMouseEvent):void
		{	
			this.removeOverlay(e.currentTarget as Marker);
		}
		
		public function deleteMarker (e:MapMouseEvent):void
		{
			var m:Marker = Marker(e.currentTarget);
			var point:LatLng = m.getLatLng();
			app.getPdata().unsetMarker(app.getPdata().findNearestTrackPoint(point));
			this.removeOverlay(m);
		}
		
		public function drawTrack():void
		{
			var gp:GISPoint = app.getPdata().getCurrentGISPoint(app.videoplayer.playheadTime);
			var zoom:Number = this.getZoom();
			if (zoom==1)
				zoom = 16;			
			this.setCenter(new LatLng(gp.getLat(),gp.getLon()), zoom, MapType.SATELLITE_MAP_TYPE);
			trackline = new Polyline(app.getPdata().getPointArray());
			trackline.addEventListener(MapMouseEvent.CLICK, trackClicked);	
			trackline.addEventListener(MapMouseEvent.ROLL_OVER, showPoint);	
			//trackline.addEventListener(MapMouseEvent.ROLL_OUT, hidePoint);
			this.clearOverlays();
			this.addOverlay(trackline);
			mapposition = new Marker(new LatLng(gp.getLat(),gp.getLon()), new MarkerOptions( { clickable: false }));
			this.addOverlay(mapposition);
			
			var timemarkers:Array = app.getPdata().getTimeMarkers();
			var m:Marker;
			for (var i:Number=0; i<timemarkers.length; i++)
			{
				m = new Marker(timemarkers[i] ,  new MarkerOptions( { fillStyle: { color: 0x0055ff }, tooltip:"Time marker" , clickable: true  } ));
				m.addEventListener(MapMouseEvent.CLICK, deleteMarker);
				this.addOverlay( m );
			}	
			var infomarkers:Array = app.getPdata().getInfoBoxMarkers();
			for (i=0; i<infomarkers.length; i++)
			{
				m = new Marker(infomarkers[i] ,  new MarkerOptions( { fillStyle: { color: 0xff55 }, tooltip:"Infobox" , clickable: true  } ));
				m.addEventListener(MapMouseEvent.CLICK, gotoMarker);
				this.addOverlay( m );
			}
			
		}
		
		public function refreshTrack():void
		{
			this.removeOverlay(trackline);
			drawTrack();
		}		
		
		public function gotoMarker(e:MapMouseEvent):void
		{
			var m:Marker = Marker(e.currentTarget);
			var point:LatLng = m.getLatLng();
			app.getVideoPlayer().playheadTime = app.getPdata().getTimeAt(app.getPdata().findNearestTrackPoint(point));	
		}

		public function gotoPoint(point:LatLng):void
		{
			app.getVideoPlayer().playheadTime = app.getPdata().getTimeAt(app.getPdata().findNearestTrackPoint(point));
		}

		public function setMapPosition(lat:Number, lng:Number):void		
		{
			var pos:LatLng = new LatLng(lat, lng);
			this.panTo(pos);
			mapposition.setLatLng(pos);
		}
		
		public function close():void 
		{
			if (this.mapready)
			{
				this.clearOverlays(); 		
				this.setCenter(new LatLng(0,0), 1, MapType.NORMAL_MAP_TYPE);
			}
		}
		
		public function deletePoint(pos:LatLng):void
		{
			var index:int = app.getPdata().findNearestTrackPoint(pos);
			app.getPdata().deleteTrackPoint(index);
			trace("deleting point");
		}
		
		private function mapMenuListener(e:Event):void
		{			
			var option:Number = e.currentTarget.getSelectedOption();
			trace("option selected = "+option);
			var pos:LatLng = e.currentTarget.getLocation();
			if (option == 0)
				changePointTime(pos, true);
			else if (option == 1)
				changePointTime(pos, false);
			else if (option == 2)
				deletePoint(pos);
			else if (option == 3)
			{
				
			}
			else if (option == 4)
				gotoPoint(pos);	
		}
				
	}
}